﻿using UnityEngine;
using System.Collections;
using System.IO;

public class FileLog : MonoBehaviour
{
    private static FileLog instance;
    private StreamWriter writer;
    private StreamWriter Writer
    {
        get
        {
            if(writer == null)
            {
                System.DateTime dt = System.DateTime.Now;
                writer = new StreamWriter(Application.dataPath + "/../" +dt.Year+"_"+dt.Month+"_" + dt.Day+" " +dt.Hour +"."+ dt.Minute + "."+ dt.Second+ ".txt");
            }
            return writer;
        }
    }

    void Awake()
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }

    public static void Log(string msg)
    {
#if UNITY_EDITOR
        instance.Writer.WriteLine(msg);
        instance.Writer.Flush();
        //UnityEngine.Debug.Log("<color=yellow>" + msg + "</color>");
#endif
    }

    public void OnApplicationQuit()
    {
#if UNITY_EDITOR
        if (writer != null)
        {
            writer.Flush();
            writer.Close();
        }
#endif
    }

}
